package com.play.ballen.synthetic.ui

import android.view.View
import android.widget.Button
import androidx.annotation.NonNull
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.play.ballen.R
import com.play.ballen.databinding.FragmentSyntheticBinding
import com.play.ballen.event.EventCost
import com.play.ballen.synthetic.adapter.SyntheticBallAdapter
import com.play.ballen.synthetic.adapter.SyntheticGuessAdapter
import com.play.ballen.synthetic.dialog.GuessDialogUtils
import com.play.ballen.synthetic.dialog.NoticeDialogUtils
import com.play.ballen.synthetic.entity.Guess
import com.play.ballen.synthetic.entity.GuessInfo
import com.play.ballen.synthetic.entity.SyntheticBallInfo
import com.play.ballen.synthetic.vm.SyntheticVM
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.appframework.widget.SpacesItemDecoration
import com.yuven.baselib.component.BaseMVVMFragment
import com.yuven.baselib.component.eventbus.Event
import com.yuven.baselib.utils.ViewUtils
import com.yuven.baselib.widget.MultiStateView


class SyntheticFragment : BaseMVVMFragment<SyntheticVM, FragmentSyntheticBinding>() {

    private var isLoadData = false


    private var curEntity: SyntheticBallInfo? = null

    private var guess: Guess? = null

    private val adapter: SyntheticBallAdapter by lazy {
        SyntheticBallAdapter(arrayListOf())
    }

    private val guessAdapter: SyntheticGuessAdapter by lazy {
        SyntheticGuessAdapter(arrayListOf())
    }

    private val noticeDialogUtils by lazy {
        NoticeDialogUtils(mContext).apply {
            numberCallback = { count ->

            }
        }
    }

    private val guessDialogUtils by lazy {
        GuessDialogUtils(mContext).apply {
            numberCallback = { count ->
                val map = mutableMapOf<String, Any>()
                map["level"] = guess!!.level
                map["nums"] = count
//                map["__token__"] = viewModel.rateInfo.value!!.__token__
                viewModel.syntheticGuess(map)
            }
        }
    }



    companion object {
        val TAG: String = SyntheticFragment::class.java.simpleName

        fun newInstance(): SyntheticFragment {
            return SyntheticFragment()
        }
    }

    override fun initIntent() {}

    override fun layoutId(): Int {
        return R.layout.fragment_synthetic
    }

    override fun viewModelClass(): Class<SyntheticVM> {
        return SyntheticVM::class.java
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.notice.observe(this) {
            if(it.status!=0){
                noticeDialogUtils.show(it.status)
            }
        }


        //投票完毕
        viewModel.actionGuessSuccess.observe(this) {
            viewModel.getGuessInfoList()
        }
        viewModel.actionGetBallFailure.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_ERROR
        }

        //列表的数据
        viewModel.listGuess.observe(this) {
            guess
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()
            dataBinding.dayAmount.text = it.desctry_amount.toString()
            dataBinding.guessCount.text = it.all_join_num.toString()
            if (!it.guess_list.isEmpty()) {
                adapter.setList(it.guess_list)
                dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_CONTENT
            } else {
                dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_EMPTY
            }
        }

        viewModel.syntheticInfo.observe(this) {
            it?.run {
                SyntheticActivity.start(mContext, guess?.basketball?.id.toString(), this)
            }
        }


        viewModel.rateInfo.observe(this) {
            it?.run {
                guess?.let { it1 ->
                    guessDialogUtils.show(1.0.toFloat(), rate, 20.0.toFloat(),
                        it1.price)
                }
            }
        }
        //guessDialogUtils.show(0.3.toFloat(),  1.2.toFloat())
    }

    override fun initView() {
        dataBinding.fakeStatusBar.layoutParams.height = ViewUtils.getStatusHeight()
        dataBinding.msvLayout.findViewById<Button>(R.id.btnTryAgain).setOnClickListener {
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
            viewModel.getGuessInfoList()
        }

        //投票记录
        dataBinding.guessRecord.setOnSingleClickListener {
            NewSyntheticRecordActivity.start(mContext,0)
        }
        //合成记录
        dataBinding.imgRecord.setOnSingleClickListener {
            NewSyntheticRecordActivity.start(mContext,1)
        }
        //中签记录
        dataBinding.winRecord.setOnSingleClickListener {
            NewSyntheticRecordActivity.start(mContext,2)
        }


//        dataBinding.rvGuess.findViewById<Button>(R.id.btnGuess).setOnClickListener {
//            viewModel.basketballBuyRate()
//            guessDialogUtils.show(1.0.toFloat(), 1.0.toFloat(), 20.0.toFloat())
//        }
    }



    override fun initData() {
        dataBinding.refreshLayout.setEnableLoadMore(false)
        dataBinding.refreshLayout.setOnRefreshListener {
            viewModel.getGuessInfoList()
        }

        dataBinding.rv.layoutManager = GridLayoutManager(mContext, 2)
        dataBinding.rv.addItemDecoration(SpacesItemDecoration(2, ViewUtils.dip2px(10f), true))
        dataBinding.rv.adapter = adapter
//        adapter.addChildClickViewIds(R.id.btn)
//        adapter.setOnItemChildClickListener { a, view, position ->
//            curEntity = adapter.data[position]
//            viewModel.getSyntheticInfo(curEntity!!.id)
//        }

        adapter.setOnItemChildClickListener(object : OnItemChildClickListener {
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int
            ) {
                when (view.getId()) {
                    R.id.btnDoGuess -> {
                        guess = viewModel.listGuess.value?.guess_list?.get(position)
                        viewModel.basketballBuyRate()
                    }
                    R.id.btn_hecheng -> {
                        guess = viewModel.listGuess.value?.guess_list?.get(position)
                        viewModel.getSyntheticInfo(guess?.basketball?.id.toString())
                    }
                }
            }
        })

    }

    override fun registerEventBus(): Boolean {
        return true
    }

    override fun handleEvent(event: Event<Any>) {
        super.handleEvent(event)
        when (event.code) {
            EventCost.CODE_SYNTHETIC_SUCCESS -> {
                dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
                viewModel.getGuessInfoList()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        //获取最新中奖通知
        viewModel.getNotice()
        if (!isLoadData) {
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
            viewModel.getGuessInfoList()
            //viewModel.getSyntheticGuessInfo(curEntity!!.id)
            isLoadData = true
        }
    }
}